package com.crm.system.controller.verifyPhone;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.verifyPhone.VerifyPhone;
import com.crm.model.qo.cluePoolManager.CustomerOnlineFollowQO;
import com.crm.model.qo.verifyPhone.VerifyPhoneQO;
import com.crm.model.qo.verifyPhone.VerifyPhoneReportQO;
import com.crm.model.vo.verifyPhone.VerifyPhoneVO;
import com.crm.system.service.verifyPhone.VerifyPhoneService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author renshaorong
 * @Date 2021/7/7
 */
@RestController
@RequestMapping("/verifyPhone")
@Slf4j
public class VerifyPhoneController {

    @Autowired
    private VerifyPhoneService verifyPhoneService;

    /**
     * 获取分页列表
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param verifyPhoneQO
     * @return
     */
    @PostMapping("/getVerifyPhoneList")
    public DataResponse getVerifyPhoneList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                           @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                           @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                           @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                           VerifyPhoneQO verifyPhoneQO) {
        DhPageInfo<VerifyPhoneVO> verifyPhoneVOs = verifyPhoneService.getVerifyPhoneList(pageIndex,pageSize,sortName,sortOrder,verifyPhoneQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), verifyPhoneVOs);
    }

    /**
     * 拨打核实电话
     * @Author pym
     * @Date  2021/7/9 10:53
     * @param request
     * @param customerOnlineFollowQO 电话信息
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/callVerifyPhone")
    public DataResponse callVerifyPhone(HttpServletRequest request,
                                        @RequestBody CustomerOnlineFollowQO customerOnlineFollowQO) {
        try {
            String result = verifyPhoneService.callVerifyPhone(customerOnlineFollowQO);
            if (StringUtils.isNotBlank(result)) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("verifyPhone callVerifyPhone error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 编辑信息
     * @param verifyPhone
     * @return
     */
    @PostMapping("/updateVerifyPhoneInfo")
    public DataResponse updateVerifyPhoneInfo(@RequestBody VerifyPhone verifyPhone){
        return new DhResponse(verifyPhoneService.updateVerifyPhoneInfo(verifyPhone) ? ResponseCodeEnum.OK.getResponseCode() : ResponseCodeEnum.ERROR.getResponseCode());
    }

    /**
     * 获取企业列表
     * @return
     */
    @PostMapping("/getCustomerList")
    public DataResponse getCustomerList(){
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),verifyPhoneService.getCompCustomerList());
    }

    /**
     * 关联企业
     * @param verifyId
     * @param customerId
     * @return
     */
    @PostMapping("/bindCustomer")
    public DataResponse bindCustomer(@RequestParam(value = "verifyId", required = true) String verifyId,
                                     @RequestParam(value = "customerId", required = true) String customerId,
                                     @RequestParam(value = "contactName", required = false) String contactName){
        return new DhResponse(verifyPhoneService.bindCustomer(verifyId,customerId,contactName) ? ResponseCodeEnum.OK.getResponseCode() : ResponseCodeEnum.ERROR.getResponseCode());
    }




    /**
     * @Description: 电话核实柱状图报表 - 管理端报表
     * @Param: [reportQO]
     * @Return: com.crm.common.model.DataResponse
     * @Author: lixq-f
     * @Date: 2021/7/13
    **/
    @PostMapping("/getVerifyPhoneReport")
    public DataResponse getVerifyPhoneReport(VerifyPhoneReportQO reportQO) {
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),verifyPhoneService.getVerifyPhoneReport(reportQO));
    }

    /**
     * 获取通话录音
     * @param verifyId
     * @return
     */
    @PostMapping("/getCallRecord")
    public DataResponse getCallRecord(String verifyId) {
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),verifyPhoneService.getCallRecord(verifyId));
    }

    /**
     * 核实电话 管理端表格数据，返回分公司下个人 <30 <60 <90 >90 总时长数据
     * @param dateUnit
     * @return
     */
    @PostMapping("/getDurationByCompanyId")
    public DataResponse getDurationByCompanyId(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,Integer dateUnit){
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),verifyPhoneService.getDurationByCompanyId(pageIndex,pageSize,dateUnit));
    }

    /**
     * 核实电话 超管端表格数据，返回各分公司下 <30 <60 <90 >90 总时长数据 平均数
     * @param dateUnit
     * @return
     */
    @PostMapping("/getAllDurationGroupByCompanyId")
    public DataResponse getAllDurationGroupByCompanyId(Integer dateUnit){
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),verifyPhoneService.getAllDurationGroupByCompanyId(dateUnit));
    }

}
